/*
 * archmage
 * version: 0.1.0
 * IdFactory.java
 * Copyright by codefish, zeyu@ufl.edu
 * 2012
 */
package util;

import log.PVLogger;
import mrsimulator.Job;

import java.util.*;

/**
 * @author codefish
 *
 */
public class IdFactory {
    private static PVLogger LOG = PVLogger.getLogger(IdFactory.class);
    private static String PNODE_PREFIX = "PNODE_";
    private static String VNODE_PREFIX = "_VNODE_";
    private static long numOfPNode = 0;
    private static ArrayList<Integer> numOfVNodeOn = new ArrayList<Integer>();
    private static String IP_PREFIX = "192.168.";
    private static short third = 0;
    private static short forth = 0;
    private static final String JOB_ID_PREFIX = "JOB_";
    private static Map<String, Integer> mapNumbers = new HashMap<String, Integer>();
    private static Map<String, Integer> reduceNumbers = new HashMap<String, Integer>();

 public static long getPNodeId() {
        long ret = 0;
        synchronized (IdFactory.class){
            ret = numOfPNode++;
            numOfVNodeOn.add(0);
        }
        return ret;
    }

    public static String getVNodeId(long pNodeId) {
        String ret = null;
        synchronized (IdFactory.class){
            if(pNodeId >= numOfVNodeOn.size()){
                LOG.error("pnode doesn't exist");
            } else {
                int idx = (int) pNodeId;
                Integer numOfVNode = numOfVNodeOn.get(idx); // TODO: FIXME, in fact we cannot cast like this
                ret = new String(PNODE_PREFIX + pNodeId + VNODE_PREFIX + numOfVNode.intValue());
                numOfVNodeOn.set(idx, numOfVNode+1);
            }
        }
        return ret;
    }

    public static String getNextIp() {
        String ret = null;
        synchronized (IdFactory.class){
            ret = new String (IP_PREFIX + third + "." + forth);
            if(forth == 254){
                forth = 0;
                third ++;
            } else {
                forth ++;
            }
        }
        return ret;
    }
    public static String getJobId(){
        return JOB_ID_PREFIX + Time.getTime();
    }

    public static String getTaskId(Job j, boolean isMap){
        String jid = j.getJobId();
        synchronized (IdFactory.class){
            if(isMap){
                Integer currentMapNumber = mapNumbers.get(jid);
                if(currentMapNumber == null){
                    mapNumbers.put(jid, 0);
                    return j.getJobId() + "_m_" + 0;
                } else {
                    mapNumbers.put(jid, currentMapNumber+1);
                    return j.getJobId() + "_m_" + (currentMapNumber+1);
                }
            } else {
                Integer currentReduceNumber = reduceNumbers.get(jid);
                if(currentReduceNumber == null){
                    reduceNumbers.put(jid, 0);
                    return j.getJobId() + "_r_" + 0;
                } else {
                    reduceNumbers.put(jid, currentReduceNumber+1);
                    return j.getJobId() + "_r_" + (currentReduceNumber+1);
                }
            }
        }
    }
}
